package com.hly.musicbackground.model.index.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Enum.BannerEnum;
import com.hly.musicbackground.Enum.UserLogEnum;
import com.hly.musicbackground.dao.BannerDao;
import com.hly.musicbackground.domain.Banner;
import com.hly.musicbackground.model.index.service.BannerService;
import com.hly.musicbackground.model.user.service.UserLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BannerServiceImpl extends ServiceImpl<BannerDao, Banner> implements BannerService {

    private final UserLogService userLogService;

    @Override
    public List<Banner> getBannerList(String type) {
        return lambdaQuery()
                .eq(Banner::getStatus, BannerEnum.SHOW.getValue())
                .eq(Banner::getType, type).list();
    }

    @Override
    public void setStatus(int id) {
        Banner byId = getById(id);
        byId.setStatus(byId.getStatus() == 0 ? 1 : 0);
        updateById(byId);
        userLogService.addUserLogOrFail(byId.getStatus() == 0 ? UserLogEnum.ENABLE_BANNER : UserLogEnum.DISABLE_BANNER);
    }

    @Override
    public Banner saveOrSetBanner(Banner banner) {
        if (banner.getBannerId() == 0){
            save(banner);
            userLogService.addUserLogOrFail(UserLogEnum.SAVE_BANNER);
        }else {
            updateById(banner);
            userLogService.addUserLogOrFail(UserLogEnum.SET_BANNER);
        }
        return getById(banner.getBannerId());
    }
}
